package com.qianbei.common.dialog;

import android.app.DialogFragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;

import com.qianbei.R;
import com.qianbei.common.util.ConstantValue;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by user on 16/9/29.
 */

public class CommonQuestionDialog extends DialogFragment {

    private ListView mListView;
    private CommonMenuAdapter mAdapter;
    private List<Integer> mItems = new ArrayList<>();
    private DialogItemOnClickListener mListener;

    private int mMenuTextColor;
    private int mMenuPosition = -1;

    private Object mClickData;

    public static CommonQuestionDialog newInstance(ArrayList<Integer> data) {

        Bundle args = new Bundle();
        args.putIntegerArrayList(ConstantValue.DATA_KEY,data);

        CommonQuestionDialog fragment = new CommonQuestionDialog();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setStyle(STYLE_NORMAL,R.style.custom_dialog);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.common_question_dialog,container,false);
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        Bundle bundle = getArguments();
        if (bundle != null){
            mItems = bundle.getIntegerArrayList(ConstantValue.DATA_KEY);
        }

        mListView = (ListView) view.findViewById(R.id.common_list_view);

        mAdapter = new CommonMenuAdapter(getActivity(),mItems, R.layout.dialog_item_common_question);
        mAdapter.setMenuTextColorByPosition(mMenuPosition,mMenuTextColor);
        mListView.setAdapter(mAdapter);

        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                if (mListener != null){
                    mListener.dialogOnItemClick(mItems.get(position),mClickData);
                    dismiss();
                }
            }
        });

        view.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {

                if (v instanceof ListView){
                    return false;
                }else{
                    dismiss();
                    return true;
                }
            }
        });
    }


    public interface DialogItemOnClickListener{
        void dialogOnItemClick(int itemId, Object object);
    }

    public void setItemOnClickListener(DialogItemOnClickListener listener){
        this.mListener = listener;
    }

    /**
     * 设置menu菜单 文字颜色
     *
     * @param position
     * @param color
     */
    public void setMenuTextColorByPosition(int position,int color){
        this.mMenuTextColor = color;
        this.mMenuPosition = position;
    }

    @Override
    public void onDetach() {
        super.onDetach();
        mListener = null;
    }

    public void setObjectData(Object objectData){
        this.mClickData = objectData;
    }
}
